package com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.share.Share;
import com.coffeeloversupportlibrary.databinding.ShareItemBinding;

import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/14 19:02
 * @projectName CoffeeLoverSupportLibrary
 * @className ShareAdapter.java
 * @description TODO
 */
public class ShareAdapter extends RecyclerView.Adapter {
    private ShareItemBinding shareItemBinding;

    private List<Share> dataSource;

    public ShareAdapter(List<Share> dataSource) {
        this.dataSource = dataSource;
    }

    public List<Share> getDataSource() {
        return dataSource;
    }

    public void setDataSource(List<Share> dataSource) {
        this.dataSource = dataSource;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.share_item,parent,false);
        shareItemBinding= DataBindingUtil.bind(view);
        return new ShareViewHolder(shareItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Share share=dataSource.get(position);

        ((ShareViewHolder)holder).shareItemBinding.shareItemContent.setText(share.getContentText());

        Glide.with( ((ShareViewHolder)holder).shareItemBinding.getRoot().getContext()).load(share.getImageUrl()).into( ((ShareViewHolder)holder).shareItemBinding.shareItemImage);

    }

    @Override
    public int getItemCount() {
        return dataSource.size();
    }

    static class ShareViewHolder extends RecyclerView.ViewHolder{
        ShareItemBinding shareItemBinding;
        public ShareViewHolder(@NonNull ShareItemBinding shareItemBinding) {
            super(shareItemBinding.getRoot());
            this.shareItemBinding=shareItemBinding;
        }
    }
}
